<template>
  <div class="login-setting">
    <el-form
      ref="userForm"
      :model="userInfo"
      label-width="120px"
      :rules="rules"
      style="margin-left: 120px; margin-top: 30px"
    >
      <el-form-item label="姓名: " prop="username">
        <el-input v-model="userInfo.username" style="width:300px" />
      </el-form-item>
      <el-form-item label="密码: " prop="password">
        <el-input v-model="userInfo.password" type="password" style="width:300px" show-password />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="updateFn">更新</el-button>
        <el-button @click="$router.push('/employees')">取消</el-button>
      </el-form-item>

    </el-form>
  </div>
</template>
<script>
export default {
  name: 'LoginSetting',
  props: {
    userDetail: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      userInfo: {
        username: '',
        password: ''

      },
      rules: {
        username: [{ required: true, message: '用户名不能为空', triggler: 'blur' }],
        password: [{ required: true, message: '密码不能为空', triggler: 'blur' }]
      }
    }
  },
  // 监听 父组件传过来数据  统一传给this.userInfo
  watch: {
    userDetail() {
      // 为什么不分着传  为了保证下面请求时候 userInfo里要有更多的参数名和值(包括id)
      this.userInfo = this.userDetail
    }
  },
  methods: {
    updateFn() {
      this.$refs.userForm.validate(valid => {
        if (!valid) return
        this.$emit('saveEmpDetailByIdFn', { ...this.userInfo })
        // 浅拷贝是为了防止表单置空 影响左边使用对象值问题
      })
    }
  }
}
</script>
<style lang="sass" scoped>

</style>
